package com.wk.warehouse.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.Statistics;
import com.wk.warehouse.entity.StoreCheck;
import com.wk.warehouse.service.StatisticsService;
import com.wk.warehouse.service.StoreCheckService;
import com.wk.warehouse.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
	
	@Autowired
	private StatisticsService statisticsService;
	@Autowired
	private StoreCheckService storeCheckService;

	/**
	 * 下架商品信息
	 */
	@GetMapping("/product-out")
	public Result outProduct(Integer storeId){
		List<Statistics> outList = statisticsService.statOutProduct(storeId);
		return Result.ok(outList);
	}

	/**
	 * 库存不足的商品列表
	 */
	@RequestMapping("/understock")
	public ModelAndView exportUnderstock(HttpServletRequest request){
		ModelAndView mav = null;
		String storeID = request.getParameter("storeId");
		int storeId = 0;
		if(StringUtils.isNotBlank(storeID)){
			storeId = Integer.parseInt(storeID);
		}
		List<Statistics> underList = statisticsService.statUnderStock(storeId);
		if(underList.size() >0){
			mav = new ModelAndView("understock-download");
			mav.addObject("underList", underList);			
		}else{
			mav = new ModelAndView("404");
		}
		return mav;
	}

	/**
	 * 各仓库的商品库存情况
	 */
	@GetMapping("/store-invent")
	public Result storeInvent(){
		List<Statistics> statisticsList = statisticsService.storeInvent();
		return Result.ok(statisticsList);
	}

	/**
	 * 规定时间内的采购量
	 */
	@RequestMapping("/purchase")
	public ModelAndView exportPurchase(Statistics stat){
		ModelAndView mav = null;
		List<Statistics> purList =  statisticsService.statPurchase(stat);
		if(purList.size()>0){
			mav = new ModelAndView("purchase-download");
			mav.addObject("purList", purList);
		}else{
			mav = new ModelAndView("404");
		}
		return mav;
	}

	/**
	 * 统计列表
	 */
	@GetMapping("/list")
	public Result statisticsList(StoreCheck storeCheck, Page page){
		/*storeCheck.setCheckState("1");
		Page page1 = storeCheckService.getPageFromStoreCheck(storeCheck,page);
		List<?> checkList = page1.getResultList();
		mav.addObject("checkList", checkList);*/
		return Result.ok();
	}

	/**
	 * 规定批次的损耗比
	 */
	@RequestMapping("/lossRatio")
	public ModelAndView exportLossRatio(HttpServletRequest request){
		String storeID = request.getParameter("storeId");
		String batchID = request.getParameter("batchId");
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		int storeId = 0;
		int batchId = 0;
		if(StringUtils.isNotBlank(storeID)){
			storeId = Integer.parseInt(storeID);
		}
		if(StringUtils.isNotBlank(batchID)){
			batchId = Integer.parseInt(batchID);
		}
		hashMap.put("storeId", storeId);
		hashMap.put("batchId", batchId);
		List<Statistics> lossList = statisticsService.statLossRatio(hashMap);
		ModelAndView mav = null;
		if(lossList.size() > 0){
			mav = new ModelAndView("lossRatio-downLoad");
			mav.addObject("lossList", lossList);
		}else{
			mav = new ModelAndView("404");
		}
		return mav;
	}

	/**
	 * 规定时间内的销售比
	 */
	@RequestMapping("/marketRatio")
	public ModelAndView exportMarketRatio(Statistics stat){
		ModelAndView mav = null;
		List<Statistics> marketList = statisticsService.statMarketRatio(stat);
		if(marketList.size()>0){
			mav = new ModelAndView("market-download");
			mav.addObject("marketList", marketList);
		}else{
			mav = new ModelAndView("404");
		}
		return mav;
	}

}
